<?php
namespace app\system\controller;

use app\common\model\ShopAttrItemModel;
use app\common\model\ShopAttrModel;
use app\common\model\ShopGoodsModel;
use think\Request;

class ShopAttrController extends BaseController
{

    private $attrModel;
    private $attrItemModel;

    /**
     * 初始化
     */
    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->attrModel     = new ShopAttrModel();
        $this->attrItemModel = new ShopAttrItemModel();
    }

    /**
     * 首页
     */
    public function index()
    {
        $map      = [];
        $query    = [];
        $map['site_id'] = $this->getSiteId();
        $query['site_id'] = $this->getSiteId();
        $attrName = $this->request->param('attr_name');
        if (!empty($attrName)) {
            $map['attr_name']   = ['like', '%' . $attrName . '%'];
            $query['attr_name'] = $attrName;
        }
        $list = $this->attrModel->where($map)->order('attr_sort asc')->paginate(10, false, [
            'query' => $query,
        ]);
        $this->assign('list', $list);
        $this->assign('query', $query);
        return $this->fetch();
    }

    /**
     * 添加
     * @return mixed
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $data   = $this->request->param();
            $data['site_id'] = $this->getSiteId();
            $result = $this->validate($data, 'ShopAttrValidate');
            if ($result !== true) {
                $this->error($result);
            }
            //主表数据
            $this->attrModel->addData($data);
            //从表数据
            if ($this->request->has('item')) {
                $item = $this->request->param('item/a');
                foreach ($item as $k => $v) {
                    $item[$k]['attr_id'] = $this->attrModel->attr_id;
                }
                $this->attrItemModel->saveAll($item);
            }
            $this->success('添加成功', url('index'));
        }
        $this->assign('utype',$this->getUtype());
        return $this->fetch();
    }

    /**
     * 修改
     * @return mixed
     */
    public function edit()
    {
        if ($this->request->isAjax()) {
            $data   = $this->request->param();
            $result = $this->validate($data, 'ShopAttrValidate');
            if ($result !== true) {
                $this->error($result);
            }
            //主表数据
            $this->attrModel->editData($data);
            //从表数据
            if ($this->request->has('item')) {
                $item = $this->request->param('item/a');
                foreach ($item as $k => $v) {
                    if ($v['item_id'] == 0) {
                        unset($item[$k]['item_id']);
                    }
                    $item[$k]['attr_id'] = $this->attrModel->attr_id;
                }
                $this->attrItemModel->saveAll($item);
            }
            $this->success('操作成功！', url('index'));
        }
        $attrId   = $this->request->param('attr_id');
        $info     = $this->attrModel->where('attr_id', $attrId)->find();
        $itemList = $this->attrItemModel->where('attr_id', $attrId)->order('item_sort asc')->select();
        $this->assign('utype',$this->getUtype());
        $this->assign('itemList', $itemList->toJson());
        $this->assign('info', $info);
        return $this->fetch();
    }

    /**
     * 删除
     */
    public function del()
    {
        if ($this->request->isAjax()) {
            $goodsModel = new ShopGoodsModel();
            $attrId     = $this->request->param('attr_id/d');
            $goodsInfo  = $goodsModel->where("find_in_set('" . $attrId . "',attr_ids)")->find();
            if ($goodsInfo) {
                $this->error('该规格正在使用，不允许删除！');
            }
            $this->attrModel->where('attr_id', $attrId)->delete();
            $this->attrItemModel->where('attr_id', $attrId)->delete();
            $this->success('删除成功！');
        }
    }

    /**
     * 删除属性值
     */
    public function delItem()
    {
        $itemId     = $this->request->param('item_id/d');
        $itemInfo   = $this->attrItemModel->find($itemId);
        $goodsModel = new ShopGoodsModel();
        $goodsInfo  = $goodsModel->alias('t1')
            ->join("shop_goods_sku t2", "t1.goods_id=t2.goods_id")
            ->where("find_in_set('" . $itemInfo['attr_id'] . "',attr_ids)")
            ->where("find_in_set('" . $itemInfo['item_name'] . "',sku_name)")
            ->find();
        if ($goodsInfo) {
            $this->error('该规格值正在使用，不允许删除！');
        }
        $this->attrItemModel->where('item_id', $itemId)->delete();
        $this->success('操作成功！');
    }

    /**
     *排序
     */
    public function updateOrder()
    {
        $listOrder = $this->request->param('list_order/a');
        foreach ($listOrder as $attr_id => $attr_sort) {
            $attrId     = intval($attr_id);
            $attrSort   = intval($attr_sort);
            $dataList[] = ["attr_id" => $attrId, "attr_sort" => $attrSort];
        }
        $this->attrModel->saveAll($dataList);
        $this->success('排序成功！');
    }
}
